<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>
        {% if list_type == 'new' %}
        最新房源列表 - 智能租房
        {% else %}
        热点房源列表 - 智能租房
        {% endif %}
    </title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background: #f8f9fa;
            padding: 20px;
        }
        .list-container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
            padding: 30px;
        }
        .collection-line {
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
            transition: all 0.3s ease;
            background: white;
        }
        .collection-line:hover {
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
            transform: translateY(-2px);
        }
        .img-box {
            width: 100%;
            height: 180px;
            object-fit: cover;
            border-radius: 6px;
        }
        .title {
            font-size: 1.2em;
            font-weight: bold;
            margin-bottom: 15px;
            line-height: 1.4;
        }
        .title a {
            color: #2c3e50;
            text-decoration: none;
        }
        .title a:hover {
            color: #3498db;
        }
        .attribute-text {
            color: #7f8c8d;
            font-weight: bold;
            min-width: 80px;
            display: inline-block;
        }
        .info-text {
            color: #2c3e50;
        }
        .info-more {
            text-align: right;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            height: 100%;
        }
        .price-text {
            color: #e74c3c;
            font-size: 1.8em;
            font-weight: bold;
            margin-bottom: 15px;
        }
        .back-button {
            margin-bottom: 20px;
        }
        .list-header {
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 2px solid #3498db;
        }
        .house-info-item {
            margin-bottom: 8px;
            display: flex;
            align-items: flex-start;
        }
        .view-count {
            color: #e74c3c;
            font-weight: bold;
        }
        .btn-view-detail {
            background: #3498db;
            border: none;
            padding: 8px 20px;
            border-radius: 5px;
            color: white;
            text-decoration: none;
            display: inline-block;
            transition: background 0.3s ease;
        }
        .btn-view-detail:hover {
            background: #2980b9;
            color: white;
        }
        /* 确保列正确对齐 */
        .collection-line .col-lg-5,
        .collection-line .col-lg-2 {
            display: flex;
            flex-direction: column;
        }
        /* 自定义分页样式 */
        .pagination-container {
            margin-top: 30px;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 8px;
            border: 1px solid #e0e0e0;
        }
        .pagination-nav {
            display: flex;
            align-items: center;
            gap: 5px;
        }
        .pagination-btn {
            padding: 8px 16px;
            border: 1px solid #ddd;
            background: white;
            color: #333;
            text-decoration: none;
            border-radius: 4px;
            font-size: 14px;
            transition: all 0.3s ease;
        }
        .pagination-btn:hover:not(.disabled) {
            background: #3498db;
            color: white;
            border-color: #3498db;
        }
        .pagination-btn.disabled {
            color: #999;
            cursor: not-allowed;
            background: #f5f5f5;
        }
        .pagination-number {
            padding: 8px 12px;
            border: 1px solid #ddd;
            background: white;
            color: #333;
            text-decoration: none;
            border-radius: 4px;
            font-size: 14px;
            transition: all 0.3s ease;
        }
        .pagination-number:hover {
            background: #3498db;
            color: white;
            border-color: #3498db;
        }
        .pagination-current {
            padding: 8px 12px;
            border: 1px solid #3498db;
            background: #3498db;
            color: white;
            border-radius: 4px;
            font-size: 14px;
        }
        .pagination-jump {
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .pagination-info {
            color: #666;
            font-size: 14px;
        }
        .pagination-jump-text {
            color: #666;
            font-size: 14px;
        }
        .pagination-input {
            width: 60px;
            padding: 6px 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            text-align: center;
            font-size: 14px;
        }
        .pagination-input:focus {
            outline: none;
            border-color: #3498db;
            box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
        }
        .pagination-confirm {
            padding: 6px 16px;
            border: 1px solid #3498db;
            background: #3498db;
            color: white;
            border-radius: 4px;
            font-size: 14px;
            cursor: pointer;
            transition: background 0.3s ease;
        }
        .pagination-confirm:hover {
            background: #2980b9;
            border-color: #2980b9;
        }
    </style>
</head>
<body>
    <div class="list-container">
        <a href="/" class="btn btn-outline-primary back-button">
            <i class="fas fa-arrow-left"></i> 返回首页
        </a>

        <div class="list-header">
            {% if list_type == 'new' %}
            <h2><i class="fas fa-home"></i> 最新房源列表</h2>
            <p class="text-muted">按照发布时间排序，最新发布的房源排在最前面</p>
            {% else %}
            <h2><i class="fas fa-fire"></i> 热点房源列表</h2>
            <p class="text-muted">按照浏览量排序，最受欢迎的房源排在最前面</p>
            {% endif %}
        </div>

        <div class="collection">
            <div id="fill-data" class="{{ page_num }}">
                {% for house in house_list %}
                <div class="row collection-line">
                    <!-- 左侧图片 -->
                    <div class="col-lg-5 col-md-5">
                        <div>
                            <a href="/house/{{ house.id }}">
                                <img class="img-fluid img-box" src="/static/img/房源.jpg" alt="{{ house.title }}">
                            </a>
                        </div>
                    </div>

                    <!-- 中间房源信息 -->
                    <div class="col-lg-5 col-md-5">
                        <div class="collection-line-info">
                            <div class="title">
                                <a href="/house/{{ house.id }}">
                                    {{ house.title | dealover if house.title else house.address }}
                                </a>
                            </div>

                            <div class="house-info-item">
                                <span class="attribute-text">房源地址:</span>
                                <span class="info-text">{{ house.address }}</span>
                            </div>

                            <div class="house-info-item">
                                <span class="attribute-text">建筑面积:</span>
                                <span class="info-text">{{ house.area }}平方米</span>
                            </div>

                            <div class="house-info-item">
                                <span class="attribute-text">房源户型:</span>
                                <span class="info-text">{{ house.rooms }}</span>
                            </div>

                            <div class="house-info-item">
                                <span class="attribute-text">房源朝向:</span>
                                <span class="info-text">{{ house.direction | dealdirection }}</span>
                            </div>

                            <div class="house-info-item">
                                <span class="attribute-text">交通条件:</span>
                                <span class="info-text">{{ house.traffic | dealdirection }}</span>
                            </div>

                            <div class="house-info-item">
                                <span class="attribute-text">
                                    <i class="fas fa-heart" style="color:#e74c3c"></i>
                                    <span class="view-count">{{ house.page_views if house.page_views else 0 }}人浏览过</span>
                                </span>
                            </div>
                        </div>
                    </div>

                    <!-- 右侧价格和按钮 -->
                    <div class="col-lg-2 col-md-2">
                        <div class="info-more">
                            <div>
                                <span class="price-text">¥{{ house.price }}</span>
                            </div>
                            <div>
                                <a href="/house/{{ house.id }}" class="btn-view-detail">
                                    <i class="fas fa-arrow-right"></i> 查看详情
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
                {% else %}
                <div class="text-center py-5">
                    <i class="fas fa-home fa-3x text-muted mb-3"></i>
                    <h4 class="text-muted">暂时没有房源信息</h4>
                    <p class="text-muted">请稍后再来查看</p>
                    <a href="/" class="btn btn-primary">返回首页</a>
                </div>
                {% endfor %}
            </div>
        </div>

        <!-- 自定义分页组件 -->
        {% if total_num > 1 %}
        <div class="pagination-container">
            <div class="d-flex justify-content-between align-items-center">
                <!-- 左侧页码导航 -->
                <div class="pagination-nav">
                    <a href="{% if page_num > 1 %}/list/{% if list_type == 'new' %}pattern{% else %}hot_house{% endif %}/{{ page_num - 1 }}{% else %}#{% endif %}"
                       class="pagination-btn {% if page_num <= 1 %}disabled{% endif %}">
                        上一页
                    </a>

                    {% set start_page = [1, page_num - 3]|max %}
                    {% set end_page = [total_num, page_num + 3]|min %}

                    {% for p in range(start_page, end_page + 1) %}
                        {% if p == page_num %}
                        <span class="pagination-current">{{ p }}</span>
                        {% else %}
                        <a href="/list/{% if list_type == 'new' %}pattern{% else %}hot_house{% endif %}/{{ p }}" class="pagination-number">{{ p }}</a>
                        {% endif %}
                    {% endfor %}

                    <a href="{% if page_num < total_num %}/list/{% if list_type == 'new' %}pattern{% else %}hot_house{% endif %}/{{ page_num + 1 }}{% else %}#{% endif %}"
                       class="pagination-btn {% if page_num >= total_num %}disabled{% endif %}">
                        下一页
                    </a>
                </div>

                <!-- 右侧跳转表单 -->
                <div class="pagination-jump">
                    <span class="pagination-info">共{{ total_num }}页，</span>
                    <span class="pagination-jump-text">到第</span>
                    <input type="number" id="jump-page" class="pagination-input" min="1" max="{{ total_num }}" value="{{ page_num }}">
                    <span class="pagination-jump-text">页</span>
                    <button type="button" id="jump-btn" class="pagination-confirm">确定</button>
                </div>
            </div>
        </div>
        {% endif %}

        <!-- 调试信息（正式环境可以删除） -->
        <div style="margin-top: 20px; padding: 10px; background: #f8f9fa; border-radius: 5px; font-size: 12px; color: #666;">
            <strong>调试信息:</strong><br>
            当前页码: {{ page_num }}<br>
            总页数: {{ total_num }}<br>
            房源总数: {{ house_num }}<br>
            当前页房源数量: {{ house_list|length }}<br>
            列表类型: {{ list_type }}
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        $(document).ready(function() {
            // 分页跳转功能
            $('#jump-btn').click(function() {
                var targetPage = parseInt($('#jump-page').val());
                var totalPages = {{ total_num }};

                // 验证输入
                if (isNaN(targetPage) || targetPage < 1) {
                    targetPage = 1;
                } else if (targetPage > totalPages) {
                    targetPage = totalPages;
                }

                // 构建跳转URL
                var baseUrl = '/list/{% if list_type == "new" %}pattern{% else %}hot_house{% endif %}/';
                window.location.href = baseUrl + targetPage;
            });

            // 输入框回车跳转
            $('#jump-page').keypress(function(e) {
                if (e.which === 13) { // Enter键
                    $('#jump-btn').click();
                }
            });

            // 输入框失去焦点时验证
            $('#jump-page').blur(function() {
                var targetPage = parseInt($(this).val());
                var totalPages = {{ total_num }};

                if (isNaN(targetPage) || targetPage < 1) {
                    $(this).val(1);
                } else if (targetPage > totalPages) {
                    $(this).val(totalPages);
                }
            });
        });
    </script>
</body>
</html>